/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.motoo.sample;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

import javax.net.SocketFactory;
import javax.net.ssl.SSLSocket;

import com.google.code.motoo.ssl.SSLHandlerFactory;

/**
 * 
 * @author linfengqi
 * @version 1.0  May 13, 2009
 */
public class SSLClient {
	
	static SSLSocket sSLSocket;

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		boolean clientMode = true;
		String keystore = "C:\\key\\server.keystore";
		String password = "tongcard";
		DataInputStream sysin=null;
		String strin=null;
		String strout=null;
		
		SSLHandlerFactory factory = new SSLHandlerFactory(clientMode, keystore, password);
		SocketFactory socketFactory = factory.getSslContext().getSocketFactory();

		sSLSocket = (SSLSocket) socketFactory.createSocket("localhost", 9000);
		PrintStream out = new PrintStream(sSLSocket.getOutputStream());
		new ReadThread(sSLSocket).start();
		while (true) {
			out.println("testtest");
			out.flush();
			Thread.sleep(2000);
		}
		
		
		
		
	}
	
	static class ReadThread extends Thread {
		private Socket socket;
		static BufferedReader in;
		private byte[] data = new byte[50];
		
		public ReadThread(Socket socket) throws IOException {
			this.socket = socket;
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		}
		
		@Override
		public void run() {
			while(true) {
		    	try {
					String content = in.readLine();
					System.out.println(content);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
	
}
